package com.zxy.supplier_system.server.xinyuan.controller

import com.zxy.supplier_system.server.service.XinyuanOrderService
import com.zxy.supplier_system.server.utils.IntIdAndName
import com.zxy.supplier_system.server.xinyuan.entity.XinyuanGoods
import com.zxy.supplier_system.server.xinyuan.entity.XinyuanOrder
import com.zxy.supplier_system.server.xinyuan.entity.toIntIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.web.PageableDefault
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal
import java.time.OffsetDateTime

@RestController
@RequestMapping("xinyuan/orders")
class XinyuanOrdersController(private val xinyuanOrderService: XinyuanOrderService) {

    data class UpdateOrderRequest(
        val name: String,
        val idCard: String? = null,
        val phoneNumber: String? = null,
        val paymentFee: BigDecimal? = null,
        val governmentSubsidies: BigDecimal? = null,
        val paymentDateTime: OffsetDateTime? = null,
        val address: String? = null,
        val unionPayOrderNumber: String? = null,
        val invoiceNumber: String? = null,
        val deliveryDateTime: OffsetDateTime? = null,
        val serialNumber: String? = null,
        val imei1: String? = null,
        val imei2: String? = null,
        val idCardFrontPicture: String? = null,
        val idCardBackPicture: String? = null,
        val productImagePictures: List<String> = emptyList(),
        val invoicePicture: String? = null,
        val unionPayInvoicePicture: String? = null,
        val deliveryNotePicture: String? = null,
        val goodsId: Int,
    )

    // 创建订单接口
    @PostMapping
    fun createOrder(@RequestBody request: UpdateOrderRequest): OrderResponse {
        return xinyuanOrderService.createOrder(request).toResponse()
    }

    // 响应类直接定义在方法下方
    data class OrderResponse(
        val id: Int,
        val name: String,
        val idCard: String?,
        val phoneNumber: String?,
        val paymentFee: BigDecimal?,
        val governmentSubsidies: BigDecimal?,
        val category: IntIdAndName,
        val brand: IntIdAndName,
        val isHomeAppliance: Boolean,
        val paymentDateTime: OffsetDateTime?,
        val address: String?,
        val energyEfficiencyLevel: XinyuanGoods.EnergyEfficiencyLevel?,
        val unionPayOrderNumber: String?,
        val invoiceNumber: String?,
        val deliveryDateTime: OffsetDateTime?,
        val barcode: String?,
        val serialNumber: String?,
        val model: String?,
        val imei1: String?,
        val imei2: String?,
        val idCardFrontPicture: String?,
        val idCardBackPicture: String?,
        val productImagePictures: List<String>,
        val invoicePicture: String?,
        val unionPayInvoicePicture: String?,
        val deliveryNotePicture: String?
    )

    // 更新订单接口
    @PutMapping("{id}")
    fun updateOrder(
        @PathVariable id: Int,
        @RequestBody request: UpdateOrderRequest
    ): OrderResponse {
        return xinyuanOrderService.updateOrder(id, request).toResponse()
    }


    data class OrderSearchCriteria(
        val isHomeAppliance: Boolean?,
        val categoryIds: List<Int>,
        val brandIds: List<Int>,
        val startDate: OffsetDateTime?,
        val endDate: OffsetDateTime?,
        val name: String?,
    )

    // 灵活查询订单接口
    @GetMapping
    fun searchOrders(
        orderSearchCriteria: OrderSearchCriteria,
        @PageableDefault(sort = ["createdDateTime"], direction = Sort.Direction.DESC) pageable: Pageable
    ): Page<OrderSummary> {

        return xinyuanOrderService.searchOrders(orderSearchCriteria, pageable)
            .map {
                OrderSummary(
                    it.id,
                    it.name,
                    it.goods.brand.toIntIdAndName(),
                    it.goods.model,
                    it.paymentDateTime,
                    it.goods.category.toIntIdAndName(),
                    it.createdDateTime
                )
            }
    }

    data class OrderSummary(
        val id: Int,
        val name: String,
        val brand: IntIdAndName,
        val model: String?,
        val paymentDateTime: OffsetDateTime?,
        val category: IntIdAndName,
        val createdDateTime: OffsetDateTime,
    )

    // 其他接口...
    @GetMapping("{id}")
    fun getOrder(@PathVariable id: Int): OrderResponse {
        return xinyuanOrderService.getOrderById(id).toResponse()
    }

    @DeleteMapping("{id}")
    fun deleteOrder(@PathVariable id: Int) {
        xinyuanOrderService.deleteOrder(id)
    }

    // 扩展函数：实体转响应
    private fun XinyuanOrder.toResponse(): OrderResponse {
        return OrderResponse(
            id = this.id,
            name = this.name,
            idCard = this.idCard,
            phoneNumber = this.phoneNumber,
            paymentFee = this.paymentFee,
            governmentSubsidies = this.governmentSubsidies,
            category = this.goods.category.toIntIdAndName(),
            brand = this.goods.brand.toIntIdAndName(),
            isHomeAppliance = this.goods.category.isHomeAppliance,
            paymentDateTime = this.paymentDateTime,
            address = this.address,
            energyEfficiencyLevel = this.goods.energyEfficiencyLevel,
            unionPayOrderNumber = this.unionPayOrderNumber,
            invoiceNumber = this.invoiceNumber,
            deliveryDateTime = this.deliveryDateTime,
            barcode = this.goods.barcode,
            serialNumber = this.serialNumber,
            model = this.goods.model,
            imei1 = this.imei1,
            imei2 = this.imei2,
            idCardFrontPicture = this.idCardFrontPicture,
            idCardBackPicture = this.idCardBackPicture,
            productImagePictures = this.productImagePictures,
            invoicePicture = this.invoicePicture,
            unionPayInvoicePicture = this.unionPayInvoicePicture,
            deliveryNotePicture = this.deliveryNotePicture
        )
    }

}